package com.yizhubao.app.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.gyf.barlibrary.ImmersionBar;
import com.weikaiyun.fragmentation.SupportActivity;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;
import com.yizhubao.app.R;
import com.yizhubao.app.app.YZBApp;
import com.yizhubao.app.utils.net.LifeCycleEvent;

import butterknife.ButterKnife;
import h.framework.pacy.utils.ToastUtil;
import io.reactivex.subjects.PublishSubject;

/**
 * 创建人: PACY
 * 创建时间: 6/4/21 9:59 AM
 * 邮箱: pengwwe457621023@qq.com
 * 类描述:
 */
public abstract class BaseActivity extends SupportActivity implements BaseView{
    //状态栏
    protected ImmersionBar mImmersionBar;

    //用于控制retrofit的生命周期，以便在destroy或其他状态时终止网络请求
    public PublishSubject<LifeCycleEvent> lifecycleSubject = PublishSubject.create();

    //该方法用于提供lifecycleSubject（相当于实现了IBaseView中的getLifeSubject抽象方法）。
    //方便Presenter中直接通过IBaseView获取lifecycleSubject，而不用每次都作为参数传递过去
    public PublishSubject<LifeCycleEvent> getLifeSubject() {
        return lifecycleSubject;
    }

    LoadingDialog dialog;
    public View notDataView;
    public View errorView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        YZBApp.getInstance().addActivity(this);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(setContentLayout());
        ButterKnife.bind(this);
        //初始化沉浸式
        if (isImmersionBarEnabled())
            initImmersionBar();
        mImmersionBar.fitsSystemWindows(false).transparentStatusBar().statusBarDarkFont(true).init();

        getErrorView();
        getNotDataView();
        initView();
        initData();
    }
    public void showLoading(String title){
        dialog = new LoadingDialog(this);
        dialog.setLoadingText(title);//设置loading时显示的文字
        dialog.show();
    }
    public void showLoading(){
        showLoading("加载中");
    }
    /**
     * 实现setContentLayout来设置布局ID
     */
    protected abstract int setContentLayout();

    protected abstract void initView();

    protected abstract void initData();

    @Override
    public void showFailure(String msg) {
        ToastUtil.showShort(msg);
        showFinished();
    }


    @Override
    public void showFinished() {
        dismissDialog();
    }

    /**
     * 关闭进度框
     */
    public void dismissDialog() {
        if (null != dialog) {
            dialog.close();
        }
    }

    /**
     * 获取网络错误view
     */
    public View getErrorView() {
        errorView = getLayoutInflater().inflate(R.layout.network_error_view, null, false);
        return errorView;
    }

    /**
     * 获取数据为空
     *
     * @return
     */
    public View getNotDataView() {
        notDataView = getLayoutInflater().inflate(R.layout.product_empty_view, null, false);
        return notDataView;
    }

    /**
     * activity跳转
     *
     * @param context
     * @param clz
     * @param bundle
     */
    public void startActivity(Context context, Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(context, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * activity跳转
     *
     * @param context
     * @param clz
     */
    public void startActivity(Context context, Class<?> clz) {
        Intent intent = new Intent();
        intent.setClass(context, clz);
        startActivity(intent);
    }


    /**
     * 初始化沉浸状态栏
     */
    protected void initImmersionBar() {
        //在BaseActivity里初始化
        mImmersionBar = ImmersionBar.with(this).statusBarDarkFont(true, 0.2f);
        mImmersionBar.init();
    }

    /**
     * 是否可以使用沉浸式
     * Is immersion bar enabled boolean.
     *
     * @return the boolean
     */
    protected boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void onPause() {
        lifecycleSubject.onNext(LifeCycleEvent.PAUSE);
        super.onPause();
    }

    @Override
    protected void onStop() {
        lifecycleSubject.onNext(LifeCycleEvent.STOP);
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        lifecycleSubject.onNext(LifeCycleEvent.DESTROY);
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
